﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework.Constraints;

namespace MerchantGalaxyGuide
{
    public class GalaxyGuide
    {
        internal Grammar InitializeData(string testDataPath)
        {
            var reader = new StreamReader(testDataPath);
            var allInfos=new List<string>();
            while (!reader.EndOfStream)
            {
                allInfos.Add(reader.ReadLine());
            }
            return BuildUpGrammarInfos(allInfos);
        }

        private Grammar BuildUpGrammarInfos(List<string> allInfos)
        {
            Grammar lastStep = null;
            foreach (var info in allInfos)
            {
                var worker=new GrammarWorker();
                Grammar grammar= worker.GetGummarInfo(info);
                if (lastStep != null)
                {
                    grammar.PassResult(lastStep);
                }
                lastStep = grammar;
                grammar.TryParse();
            }
            return lastStep;
        }
        
        public string GetAllAnswers(string testDataPath)
        {
            Grammar allGrammar=this.InitializeData(testDataPath);
            StringBuilder allAnswerStringBuilder=new StringBuilder();
            foreach (var answer in allGrammar.ParseQuestion)
            {
                allAnswerStringBuilder.AppendLine(answer.GenerateAnswer());
            }
            return allAnswerStringBuilder.ToString();
        }
    }
}
